#  函数调用自己， name这个函数就是一个递归函数
#  递归函数要点： 找规律和出口
#例
# def fun1():

#      fun1()

# 求1+2+3...+10（1，n）
sum1 = 0
for i in range(1,11):
    sum1 += i
print(sum1)

def getsum(n):
    sum1 = 0
    for i in range(1,n+1):
        sum1 += i
    return sum1
print(getsum(10))

# getsum(10) = getsum(9) +10:
# getsum(9) = getsum(8) + 9:
#.....
# getsum(1) = 1

#getsum(n) = getsum(n-1) + n

# 递归函数的出口
#  getsum(1) = 1
def getsum(n):
    if n == 1:
        return 1
    return getsum(n-1)+n

# getsum(3)
# getsum(2) + 3
# getsum(1) + 2 + 3
#  1 + 2 + 3
print(getsum(2))
# 递归函数的问题
#  递归层数太多，会导致栈内存 崩溃
# 栈<存储名字>只能存998如下：
# print(getsum(998))

# 理论上， 一切用for循环写的函数，都可以写成递归函数
#  阶乘n
# def jiecheng(n):
#     fac = 1
#     for i in range(1,n+1):
#         fac *= i
#     return fac
#
# print(jiecheng(10))

def fact(n):
    if n == 1:
        return 1
    return fact(n-1) * n
print(fact(10))

def fact(n):
    if n == 3:
        return 6
    return fact(n-1) * n
print(fact(10))
# print(fact(2))

# fib 斐波那契
# 1 1   2 3 5 8 13....
# n
def fib(n):
    if n == 1 or n == 2:
        return 1
    return fib(n-1) + fib(n-2)
print(fib(7))
print(fib(6))

# 讲故事
def story(n):
    if n == 10:
        return'你太皮了'
    return '从前有座山，山上有座庙，庙里有个老和尚给小和尚讲故事：\n'+ story(n+1)
print(story(1))

# shan = 'wuleng'
# tiandi = 'he'
# if shan == 'wuleng' and tiandi == 'he':
#     print('分手吧')
#
# def love():
#     for i in range(1,101):
#         yield 'love you:'+ str(i)
# res = love()
# for love in res:
#     print(love)

